package com.tensquare.spit.controller;

import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.spit.model.Spit;
import com.tensquare.spit.service.SpitService;
import com.tensquare.util.NotNullHelps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/spit")
public class SpitController {

    @Autowired
    private SpitService spitService ;

    @Autowired
    private RedisTemplate redisTemplate ;

    private Result result = new Result(true, StatusCode.OK);

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result deleteSpit(@PathVariable String id){
        result.setData(spitService.findById(id));
        result.setMessage("删除吐槽");
        spitService.deleteById(id);
        return result ;
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public Result updateSpit(@PathVariable String id , @RequestBody Spit spit){
        spit.set_id(id);
        result.setMessage("更新吐槽");
        spitService.update(spit);
        result.setData(true);
        return result ;
    }

    @RequestMapping(value = "/{spitId}/thumbup", method = RequestMethod.PUT)
    public Result spitThumbup(@PathVariable String spitId){
        spitService.thumbup(spitId);
        result.setMessage("吐槽点赞");
        result.setData(spitService.findById(spitId));
        return result ;
    }

    @RequestMapping(value = "/{spitId}/thumbupTemplate", method = RequestMethod.PUT)
    public Result spitThumbupTemplate(@PathVariable String spitId){
        String userId = "111";
        String redisKey = "thumbup_" + userId;
        Object value = redisTemplate.opsForValue().get(redisKey);
        if (NotNullHelps.isNotNull(value) == true) {
            result = new Result(false, StatusCode.REPERROR, "不可重复点赞");
        } else {
            spitService.thumbupTemplate(spitId);
            result.setData(spitService.findById(spitId));
            redisTemplate.opsForValue().set(redisKey, 1);
            result.setMessage("吐槽点赞[Template]");
        }
        return result ;
    }

    @RequestMapping(method = RequestMethod.POST)
    public Result saveSpit(@RequestBody Spit spit) {
        result.setMessage("保存吐槽");
        spitService.save(spit);
        result.setData(spit);
        return result ;
    }

    @RequestMapping(value = "/{id}")
    public Result findById(@PathVariable String id){
        Spit spit = spitService.findById(id);
        result.setMessage("查询成功");
        result.setData(spit);
        return result;
    }

    @RequestMapping
    public Result findAll(){
        List<Spit> spitList = spitService.findAll();
        result.setMessage("所有吐槽");
        result.setData(spitList);
        return result ;
    }

    @RequestMapping(value = "/comment/{parentId}/{page}/{size}")
    public Result findByParentId(@PathVariable String parentId ,@PathVariable Integer page , @PathVariable Integer size){
        Page<Spit> pageComment = spitService.findByParentId(parentId, page, size);
        PageResult<Spit> pageData = new PageResult<Spit>(pageComment.getTotalElements(), pageComment.getContent());
        result.setMessage("父-->子节点吐槽");
        result.setData(pageData);
        return result ;
    }


}
